package com.sync;

/**
 * @author bysun
 */

/**
 * synchronized方法与锁对象
 */
class MyObject {
    public void methodA() {
        try {
            System.out.println("begin method A threadName = " + Thread.currentThread().getName());
            Thread.sleep(2000);
            System.out.println("end");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ThreadC extends Thread {
    private MyObject myObject;

    public ThreadC(MyObject myObject) {
        this.myObject = myObject;
    }

    @Override
    public void run() {
        super.run();
        myObject.methodA();
    }
}

class ThreadD extends Thread {
    private MyObject myObject;

    public ThreadD(MyObject myObject) {
        this.myObject = myObject;
    }

    @Override
    public void run() {
        super.run();
        myObject.methodA();
    }
}


public class Demo02 {
    public static void main(String[] args) {
        MyObject myObject = new MyObject();
        ThreadC threadC = new ThreadC(myObject);
        threadC.setName("A");
        ThreadD threadD = new ThreadD(myObject);
        threadD.setName("B");
        threadC.start();
        threadD.start();
    }
}
